<template>
  <div style="margin: 20px; margin-top: 60px">
    <el-row :gutter="20">
      <!-- 搜索框左 -->
      <el-col :span="8">
        <ContentWrap>
          <el-form
            class="-mb-15px"
            :model="queryParams"
            ref="queryFormRef"
            :inline="true"
            label-width="auto"
          >
            <el-form-item :label="t('formData.witsCodeLabel')" prop="witsCode">
              <el-input
                v-model="queryParams.witsCode"
                :placeholder="t('formData.witsCodePlaceholder')"
                clearable
                @keyup.enter="handleQuery"
                class="!w-240px"
              />
            </el-form-item>
            <el-form-item>
              <el-button @click="handleQuery"
                ><Icon icon="ep:search" class="mr-5px" /> {{ t('action.search') }}</el-button
              >
              <el-button @click="resetQuery"
                ><Icon icon="ep:refresh" class="mr-5px" /> {{ t('action.reset') }}</el-button
              >
            </el-form-item>
          </el-form>
        </ContentWrap>
      </el-col>

      <!-- 搜索框右-->
      <el-col :span="16">
        <ContentWrap>
          <!-- 搜索工作栏 -->
          <el-form
            class="-mb-15px"
            :model="queryParams"
            ref="queryFormRef"
            :inline="true"
            label-width="auto"
          >
            <el-form-item :label="t('searchbar.type')" prop="deviceType">
              <el-select
                v-model="queryParams.deviceType"
                :placeholder="t('searchbar.typeInfo')"
                clearable
                class="!w-240px"
              >
                <el-option
                  v-for="dict in getStrDictOptions(DICT_TYPE.YQ_TYPE)"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>

            <el-form-item :label="t('searchbar.model')" prop="deviceMode">
              <el-select
                v-model="queryParams.deviceMode"
                :placeholder="t('searchbar.modelInfo')"
                v-if="getSelectOption"
                clearable
                class="!w-240px"
              >
                <el-option
                  v-for="dict in getSelectOption"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>

            <!--          <el-form-item :label="t('searchbar.model')"  prop="deviceMode">
            <el-select
              v-model="queryParams.deviceMode"
              filterable
              remote
              reserve-keyword
              :placeholder="t('searchbar.modelInfo')"
              clearable
              class="!w-240px"
              :remote-method="modeMethod"
              :loading="AllLoading"
              @keyup.enter="handleQuery"
            >
              <el-option
                v-for="item in modeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                @click= "modeHandleChange(item)"
              >
                <span style="float: left">仪器型号：{{ item.label }}</span>
              </el-option>
            </el-select>
          </el-form-item>-->

            <!--          <el-form-item :label="t('form.state')" prop="state">
            <el-select
              v-model="queryParams.state"
              class="!w-240px"
              clearable
              :placeholder="t('form.selectState')"
            >
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.DEVICE_STATE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>-->
            <el-form-item :label="t('searchbar.createTime')" prop="createTime">
              <el-date-picker
                v-model="queryParams.createTime"
                value-format="YYYY-MM-DD HH:mm:ss"
                type="datetimerange"
                :start-placeholder="t('form.startDate')"
                :end-placeholder="t('form.endDate')"
                :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
                class="!w-240px"
              />
            </el-form-item>
            <el-form-item>
              <el-button @click="handleQuery"
                ><Icon icon="ep:search" class="mr-5px" /> {{ t('action.search') }}</el-button
              >
              <el-button @click="resetQuery"
                ><Icon icon="ep:refresh" class="mr-5px" /> {{ t('action.reset') }}</el-button
              >
              <!--            <el-button
              type="primary"
              plain
              @click="openForm('create')"
              v-hasPermi="['base:standards:create']"
            >
              <Icon icon="ep:plus" class="mr-5px" /> {{ t('action.add') }}
            </el-button>-->
              <!--            <el-button
              type="success"
              plain
              @click="handleExport"
              :loading="exportLoading"
              v-hasPermi="['base:standards:export']"
            >
              <Icon icon="ep:download" class="mr-5px" /> {{ t('action.export') }}
            </el-button>-->
            </el-form-item>
          </el-form>
        </ContentWrap>
      </el-col>
    </el-row>

    <!-- 列表 -->
    <el-row :gutter="20">
      <!-- 列表左 -->
      <el-col :span="8">
        <ContentWrap>
          <el-table
            v-loading="loading"
            :data="listProtocol"
            :stripe="true"
            :show-overflow-tooltip="true"
          >
            <!-- WITS码列 -->
            <el-table-column :label="t('formData.witsCodeLabel')" align="center">
              <template #default="scope">
                <!-- 将数据渲染为一个按钮 -->
                <el-button type="text" @click="handleWitsCodeClick(scope.row.witsCode)">
                  {{ scope.row.witsCode }}
                </el-button>
              </template>
            </el-table-column>
            <el-table-column :label="t('formData.chMeanLabel')" align="center" prop="chMean" />
            <el-table-column :label="t('formData.unitLabel')" align="center" prop="unit">
              <template #default="scope">
                <dict-tag :type="DICT_TYPE.UNITS" :value="scope.row.unit" />
              </template>
            </el-table-column>
            <el-table-column :label="t('formData.dataTypeLabel')" align="center" prop="dataType" />
          </el-table>
          <!-- 分页 -->
          <Pagination
            :total="totalProtocol"
            v-model:page="queryParamsProtocol.pageNo"
            v-model:limit="queryParamsProtocol.pageSize"
            @pagination="getListProtocol"
          />
        </ContentWrap>
      </el-col>

      <!-- 列表右-->
      <el-col :span="16">
        <ContentWrap>
          <el-table
            v-loading="loading"
            :data="listStandard"
            :stripe="true"
            :show-overflow-tooltip="true"
          >
            <el-table-column :label="t('searchbar.type')" align="center" prop="deviceType">
              <template #default="scope">
                <dict-tag :type="DICT_TYPE.YQ_TYPE" :value="scope.row.deviceType" />
              </template>
            </el-table-column>
            <!--              <el-table-column :label="t('searchbar.model')" align="center" prop="deviceMode">
                <template #default="scope">
                  <dict-tag :type="DICT_TYPE.QY_MODEL" :value="scope.row.deviceMode" />
                </template>
              </el-table-column>-->
            <el-table-column :label="t('searchbar.model')" align="center" prop="deviceMode">
              <template #default="scope">
                <dict-tag :type="getDictType(scope.row.deviceType)" :value="scope.row.deviceMode" />
              </template>
            </el-table-column>

            <el-table-column
              :label="t('formData.serialNumberLabel')"
              align="center"
              prop="serialNumber"
            />
            <el-table-column
              :label="t('formData.coefficientKLabel')"
              align="center"
              prop="coefficientK"
            />
            <el-table-column
              :label="t('formData.coefficientBLabel')"
              align="center"
              prop="coefficientB"
            />
            <el-table-column :label="t('formData.calculMethodLabel')" prop="calculMethod">
              <template #default="scope">
                <dict-tag :type="DICT_TYPE.CALCUL_METHOD" :value="scope.row.calculMethod" />
              </template>
            </el-table-column>
            <el-table-column :label="t('formData.lengthLabel')" align="center" prop="length" />

            <el-table-column :label="t('formData.state')" align="center" prop="state">
              <template #default="scope">
                <el-tag :type="scope.row.state ? 'success' : 'danger'">
                  {{ scope.row.state ? t('action.abled') : t('action.disabled') }}
                </el-tag>
              </template>
            </el-table-column>

            <el-table-column :label="t('formData.remark')" align="center" prop="remark" />

            <el-table-column
              :label="t('formData.createTime')"
              align="center"
              prop="createTime"
              :formatter="dateFormatter"
              width="180px"
            />

            <el-table-column
              :label="t('formData.operate')"
              align="center"
              width="100px"
              fixed="right"
            >
              <template #default="scope">
                <el-button
                  link
                  type="primary"
                  @click="openForm('update', scope.row.id)"
                  v-hasPermi="['base:site:update']"
                >
                  {{ t('action.edit') }}
                </el-button>
                <!--                  <el-button
                    link
                    type="danger"
                    @click="handleDelete(scope.row.id)"
                    v-hasPermi="['base:site:delete']"
                  >
                    {{ t('action.delete') }}
                  </el-button>-->
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <Pagination
            :total="totalStandard"
            v-model:page="queryParams.pageNo"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
          />
        </ContentWrap>
      </el-col>
    </el-row>
  </div>
  <!-- 表单弹窗：添加/修改 -->
  <ProtocolsForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import ProtocolsForm from './ProtocolsForm.vue'
import { ref } from 'vue'
import { ElTable, ElTableColumn, ElButton, ElFormItem } from 'element-plus'

import { ProtocolsApi, ProtocolsVO } from '@/api/base/protocols'
import { StandardsApi, StandardsVO } from '@/api/base/standards'

/** 协议标准 列表 */
defineOptions({ name: 'Protocols' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
/*左侧协议库数据列表*/
const listProtocol = ref<ProtocolsVO[]>([]) // 列表的数据
const totalProtocol = ref(0) // 列表的总页数
/*右侧数据列表*/
const listStandard = ref<StandardsVO[]>([]) // 列表的数据
const totalStandard = ref(0) // 列表的总页数

/*左侧协议库存储表单*/
const queryParamsProtocol = reactive({
  pageNo: 1,
  pageSize: 10,
  witsCode: undefined
})
/*右侧存储表单*/
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  deviceType: undefined,
  deviceMode: undefined,
  witsCode: undefined,
  state: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表（对应左侧协议库） */
const getListProtocol = async () => {
  loading.value = true
  try {
    const data = await StandardsApi.getStandardsPage(queryParamsProtocol)
    listProtocol.value = data.list
    totalProtocol.value = data.total
  } finally {
    loading.value = false
  }
}
/** 查询列表（对应右侧） */
const getList = async () => {
  loading.value = true
  try {
    /*调用请求协议库数据信息*/
    const data = await ProtocolsApi.getProtocolsPage(queryParams)
    listStandard.value = data.list
    totalStandard.value = data.total
  } finally {
    loading.value = false
  }
}

/** 左侧协议库搜索按钮操作 */
const handleQueryProtocol = () => {
  queryParamsProtocol.pageNo = 1
  getListProtocol()
}

/** 右侧搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}
/** 左侧协议库重置按钮操作 */
const resetQueryProtocol = () => {
  queryParamsProtocol.witsCode = undefined
  handleQueryProtocol()
}
/** 右侧重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  queryParams.witsCode = undefined
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ProtocolsApi.deleteProtocols(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ProtocolsApi.exportProtocols(queryParams)
    download.excel(data, '协议标准.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 实现wits码传参到查询表单中 */
const handleWitsCodeClick = (witsCode) => {
  queryParams.witsCode = witsCode
  getList()
}

/** 初始化 **/
onMounted(() => {
  getList()
  getListProtocol()
})

/*-------------------仪器与型号表单关联部分----------------------*/
const getDictType = (deviceType) => {
  const dictTypes = {
    A: DICT_TYPE.A,
    B: DICT_TYPE.B,
    C: DICT_TYPE.C,
    D: DICT_TYPE.D,
    E: DICT_TYPE.E,
    F: DICT_TYPE.F,
    G: DICT_TYPE.G,
    H: DICT_TYPE.H
  }
  return dictTypes[deviceType] || '' // 返回对应的字典类型，如果不存在则返回空字符串
}
/*-------------------仪器与型号搜索框关联部分----------------------*/
// 计算属性，根据 queryParams.type 返回相应的选项列表
const getSelectOption = computed(() => {
  const dictTypes = {
    A: DICT_TYPE.A,
    B: DICT_TYPE.B,
    C: DICT_TYPE.C,
    D: DICT_TYPE.D,
    E: DICT_TYPE.E,
    F: DICT_TYPE.F,
    G: DICT_TYPE.G,
    H: DICT_TYPE.H
  }
  return getStrDictOptions(dictTypes[queryParams.deviceType])
})

/*/!*---------------------------获取仪器型号------------------------------*!/
const AllLoading = ref(false)
const modeList = ref<ProtocolsVO[]>([])
const modeMethod = async (query) => {
  AllLoading.value = true
  modeList.value = []
  AllLoading.value = false
  const data =  await ProtocolsApi.getModeList(query)
  modeList.value = data
}
const modeHandleChange = async (item) => {
  queryParams.deviceMode = item.value
}
//-----------------------------------------------------------------*/
</script>
